'use strict';

const Hapi = require('@hapi/hapi');
const notify = require('./sendNotify');
const notifyHandler = require('./notifyHandler.js');
const globalModule = require('./constant.js');
const {loadAllJob} = require('./Tasks')

//设置全局变量
let global = new globalModule();
notifyHandler.setGlobal(global);
const port = 3000
const init = async () => {

    const server = Hapi.server({
        port: port,
        host: '0.0.0.0'
    });
    server.route({
        method: 'GET',
        path: '/',
        handler: async (request, h) => {
            return {'msg': 'test ok'};
        }
    });
    server.route({
        method: 'POST',
        path: '/send',
        handler: async (request, h) => {
            let t = request.payload
            console.log(`${Date.now()} == > ${JSON.stringify(t)}`)
            await notify.sendNotify(t.text, t.desc)
            return {'msg': 'ok'};
        }
    });
    server.route({
        method: 'POST',
        path: '/bot',
        handler: (request, h) => {
            return notifyHandler.handler(request)
        }
    });

    await server.start();
    await loadAllJob();
    console.log('Server running on %s', server.info.uri);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();
